package com.znf4.controller.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.znf4.base.BaseReturnModel;
import com.znf4.user.car.entity.UserCar;
import com.znf4.user.car.server.UserCarServer;

/**
 * 购物车
 */
@RestController
@RequestMapping("/user/car")
public class UserCarController {
	
	@Autowired
	private UserCarServer userCarServer ;
	

	/**
	 * 函数功能说明 ： 添加购物车
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> add(UserCar userCar, Model model) {
		
		userCarServer.addUserCar(userCar);
		return BaseReturnModel.getMapModel("success", "操作完成" , "");
	}

	
	
	/**
	 * 函数功能说明 ： 修改购物车
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> update(UserCar userCar, Model model) {
		
		userCarServer.updateUserCar(userCar);
		return BaseReturnModel.getMapModel("success", "操作完成" , "");
	}

	/**
	 * 函数功能说明 ： 删除一个购物车商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delOne(UserCar userCar, Model model) {
		
		userCarServer.delOneUserCar(userCar); 
		return BaseReturnModel.getMapModel("success", "操作完成" , "");
	}

	/**
	 * 函数功能说明 ： 删除多条购物车商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delAll(String[]  ids   , Model model) {
		
		List<UserCar> list = new ArrayList<>() ;
		for(int i=0 ; i < ids.length ; i ++ ){
			UserCar userCar= new UserCar() ;
			userCar.setId(ids[i]);
			list.add(userCar) ;
		}
		userCarServer.delAllUserCar(list);
		return BaseReturnModel.getMapModel("success", "操作完成" , "");
	}
	

	/**
	 * 函数功能说明 ： 获取多个商品从购物车
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> queryAll(UserCar userCar, Model model) {
		
		List<UserCar> list = userCarServer.queryAll(userCar);
		return BaseReturnModel.getMapModel("success", list , "");
	}
	
	

}
